package com.lm.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.catalina.connector.Request;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lm.model.User;
import com.lm.service.UserService;

/**
 * 8.创建UserController类和视图文件
 * 可以看出list方法返回的是一个字符串，因为我们给应用加载了freemarker模块做视图展现，所以需要创建一个list模板，模板所在的目录在application.properties中指定为spring.freemarker.template-loader-path=classpath:/templates/，所以我们需要在src/main/resources下创建templates目录，然后在templates下创建user目录，模板文件后缀在application.properties中指定为spring.freemarker.suffix=.ftl，所以最终建立一个list.ftl文件
 * 
 * @author Administrator
 *
 */
@Controller
@RequestMapping("/user")
public class UserController {
	
	private static final Logger logger = LoggerFactory.getLogger(UserController.class);

	@Autowired
    private UserService userService;
	/**
	 * 跳转到新增信息页面
	 * @param modelMap
	 * @return
	 */
	@RequestMapping("/addInfo")
	public String addInfo(ModelMap modelMap) {
		return "/armyinfo/addInfonew";
	}
    @RequestMapping("/list")
    public String list(ModelMap map){
        List<User> userList=userService.findAll();
        map.addAttribute("userList", userList);
        return "/user/list";
    }
    /**
     * 进入到登录页面
     * @param map
     * @return
     */
    @RequestMapping("/login")
    public String login(ModelMap map){
        return "/user/login";
    }
    
    @RequestMapping("/index")
    public String index(ModelMap map){
        return "/user/index";
    }
    /*权限1跳转页面*/
    @RequestMapping("/index_main1")
    public String index_main1(ModelMap map){
    	return "/user/index_main1";
    }
    /*权限2跳转页面*/
    @RequestMapping("/index_main2")
    public String index_main2(ModelMap map) {
    	return "/user/index_main2";
    }
    /*权限3跳转页面*/
    @RequestMapping("/index_main3")
    public String index_main3(ModelMap map) {
    	return "/user/index_main3";
    }
    /*跳转到用户列表页面*/
    @RequestMapping("/userlist")
    public String userlist(ModelMap map) {
    	return "/user/userlist";
    }
    @RequestMapping("/test")
    @ResponseBody
    public String test(ModelMap map){
        return "test";
    }
    /**
     * 输入账号密码后点击登录账号密码验证
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("doLogin")
    @ResponseBody
    public Map<String, Object> doLogin(HttpServletRequest request,HttpServletResponse response){
    	Map<String, Object> resultMap = new HashMap<String, Object>();
    	String username = request.getParameter("inputEmail");
    	String pwd = request.getParameter("inputPassword");
    	User user = userService.findUserByUserName(username);
    	if (user!=null) {
			if (pwd.equals(user.getPsword())) {
				//登录成功
				resultMap.put("user", user);
				resultMap.put("code", "0");
				resultMap.put("msg", "登录成功");
				request.getSession().setAttribute("user", user);//将登陆的用户信息存储在session中
			}else {
				resultMap.put("code", "1");
				resultMap.put("msg", "密码错误");
			}
		}else {
			resultMap.put("code", "2");
			resultMap.put("msg", "用户名不存在");
		}
    	return resultMap;
    }
    /**
     * 查询用户列表
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value="findUserlist",method=RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> findUserlist(HttpServletRequest request,HttpServletResponse response){
    	Map<String, Object> resultMap = new HashMap<String, Object>();
    	try {
    		List<User> users = userService.findAll();
    		resultMap.put("code", "0");
    		resultMap.put("userList", users);
		} catch (Exception e) {
			logger.error("查询用户错误信息:"+e.getMessage());
			resultMap.put("code", "1");
		}
    	return resultMap;
    }
    /**
     * 根据用户id删除用户
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping(value="removeUser")
    @ResponseBody
    public String removeUser(HttpServletRequest request) throws Exception{
    	String id = request.getParameter("userId");
    	try {
			Integer code = userService.removeUser(Integer.parseInt(id));
			if(code==1) {
				return "ok";
			}else {
				return "no";	
			}
		} catch (Exception e) {
			return "no";
		}
    }
    /**
     * 根据用户id查询用户详情
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping(value="editUser")
    @ResponseBody
    public Map<String, Object> editUser(HttpServletRequest request) throws Exception{
    	String id = request.getParameter("userId");
    	Map<String, Object> map = new HashMap<String, Object>();
    	try {
			User user = userService.findUserById(Integer.parseInt(id));
			map.put("code", "0");
			map.put("user", user);
		} catch (Exception e) {
			map.put("code", "1");
		}
    	return map;
    }
    /**
     * 修改商户并保存
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping(value="editUserinfo")
    @ResponseBody
    public String editUserinfo(HttpServletRequest request) throws Exception{
    	String id = request.getParameter("user_id");
    	String username = request.getParameter("username");
    	String power_id = request.getParameter("power_id");
    	String part_id = request.getParameter("part_id");
    	String psword = request.getParameter("psword");
    	try {
    		User user = new User();
        	user.setUser_id(Integer.parseInt(id));
        	user.setUsername(username);
        	user.setPower_id(power_id);
        	user.setPart_id(part_id);
        	user.setPsword(psword);
        	Integer code = userService.updateUser(user);
        	if(code==1) {
        		return "ok";
        	}else {
        		return "no";
        	}
		} catch (Exception e) {
			return "no";
		}	
    }
    /**
     * 保存新增的用户
     * @return
     * @throws Exception
     */
    @RequestMapping(value="saveuserinfo")
    @ResponseBody
    public String saveuserinfo(HttpServletRequest request) throws Exception{
    	String username = request.getParameter("username");
    	String psword = request.getParameter("psword");
    	String power_id = request.getParameter("power_id");
    	String part_id = request.getParameter("part_id");
    	User user = new User(power_id,power_id,username,psword);
    	Integer code = userService.adduser(user);
    	if(code==1) {
    		return "ok";
    	}else {
    		return "no";
    	}
    }
}
